import { defineStore } from 'pinia';
import { getUserInfo } from '../api/user';
import type { UserInfo } from '../api/user';

export const useUserStore = defineStore('user', {
  state: () => ({
    userInfo: null as UserInfo | null,
    loading: false,
    error: null as string | null
  }),

  actions: {
    async fetchUserInfo() {
      this.loading = true;
      this.error = null;
      try {
        const response = await getUserInfo();
        this.userInfo = response.data;
      } catch (err) {
        this.error = '获取用户信息失败';
        console.error('获取用户信息失败:', err);
      } finally {
        this.loading = false;
      }
    },

    clearUserInfo() {
      this.userInfo = null;
      this.error = null;
    }
  }
});